import { getMemberCenterInfo, GetMemberDetail } from "@/service/api/member.js"
import { GetProductDisplay } from "@/service/api/product.js"
import store from "@/store/index.js"
import storage from "@/uni-sub-base/utils/storage.js"
import { getFunctionSettingValue } from "@/service/api/functionSetting.js"
import { getInitWebInfo } from "@/service/api/web.js"
import config from "@/config/config.js"
const appInit = {
    //获取个人信息
    getUserInfo: () => {
        return new Promise((resolve, reject) => {
            getMemberCenterInfo().then((res) => {
                if (res.Data) {
                    let obj = res.Data
                    storage.setCache("userInfo", obj)
                    //取个人详情补充到storage
                    appInit.getMemberDetail().then((member) => {
                        Object.assign(obj, member.Data)
                        storage.setCache("userInfo", obj)
                        resolve(obj)
                    })
                }
            })
        })
    },
    //获取个人详情
    getMemberDetail: () => {
        return new Promise((resolve, reject) => {
            GetMemberDetail().then((res) => {
                if (res.Data) {
                    resolve(res)
                }
            })
        })
    },
    // 获取整体显示设置
    getProductDisplay() {
        return new Promise((resolve, reject) => {
            GetProductDisplay({}).then((res) => {
                if (res.State) {
                    storage.setCache("productDisplay", res.Data)
                    resolve(res)
                }
            })
        })
    },
    getInitWebInfo(domain, pageObj,Businessld) {
        storage.removeCache("webInfo")
        console.log("Businessld", Businessld)
		if(Businessld){
			Businessld = Businessld+''
			let Businessld4 = Businessld.slice(-4)
			appInit.getInitWebInfoUp(domain, pageObj,Businessld4)
		}else{
			appInit.getInitWebInfoUp(domain, pageObj)
		}
		
    },
    getInitWebInfoUp(domain, pageObj,Businessld) {
        let webInfo = storage.getCache("webInfo")
        let oldToken
        if (webInfo && webInfo?.Token) {
            oldToken = webInfo.Token
        }
        if (domain && domain.includes("https://")) {
            domain = domain.replace("https://", "")
        }
        let param = {
            domain: domain,
			shopBusinessId:Businessld,
        }
        setTimeout((e) => {
            getInitWebInfo(param,Businessld).then((res) => {
                console.log("res", res)
                if (res.Data) {
                    res.Data.WebDomain = "https://" + domain
                    let data = res.Data
                    if (!data.Token) {
                        data.Token = oldToken
                    }
                    console.log("webInfo11", data)
                    storage.setCache("webInfo", data)
                    appInit.getFunctionSetting()
                    console.log("pageObj", pageObj)
                    if (pageObj) {
                        pageObj.setThemeColorFn(res.Data.StyleSkin)
                    }
                }
            })
        }, 200)
    },
    //获取商家配置值
    getFunctionSetting() {
        let param = {
            ids: "14,30",
        }
        getFunctionSettingValue(param).then((res) => {
            if (res.State && res.Data) {
                storage.setCache("businessFunctionSetting", res.Data)
            }
        })
    },
}
module.exports = {
    getUserInfo: appInit.getUserInfo,
    getMemberDetail: appInit.getMemberDetail,
    getProductDisplay: appInit.getProductDisplay,
    getInitWebInfo: appInit.getInitWebInfo,
    getInitWebInfoUp: appInit.getInitWebInfoUp,
    getFunctionSetting: appInit.getFunctionSetting,
}
